home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / KoGuides.h < prev    next >
Encoding:
C/C++ Source or Header  |  2007-05-30  |  13.0 KB  |  422 lines

  1. // -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4; -*-
  2. /* This file is part of the KDE project
  3.    Copyright (C) 2005 Thorsten Zachmann <zachmann@kde.org>
  4.    Copyright (C) 2005 Casper Boemann Rasmussen <cbr@boemann.dk>
  5.  
  6.    This library is free software; you can redistribute it and/or
  7.    modify it under the terms of the GNU Library General Public
  8.    License as published by the Free Software Foundation; either
  9.    version 2 of the License, or (at your option) any later version.
  10.  
  11.    This library is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.    Library General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU Library General Public License
  17.    along with this library; see the file COPYING.LIB.  If not, write to
  18.    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  19.    Boston, MA 02110-1301, USA.
  20. */
  21.  
  22. #ifndef KOGUIDES_H
  23. #define KOGUIDES_H
  24.  
  25. #include <qevent.h>
  26. #include <qobject.h>
  27.  
  28. #include <koffice_export.h>
  29.  
  30. class QPaintDevice;
  31. class KoPoint;
  32. class KoRect;
  33. class KoView;
  34. class KoZoomHandler;
  35.  
  36. class KOFFICEUI_EXPORT KoGuides : public QObject
  37. {
  38.     Q_OBJECT
  39. public:        
  40.     /**
  41.      * @brief Constructor
  42.      *
  43.      * @param view The view in which the guides will be shown
  44.      * @param zoomHandler The zoom handler of the view
  45.      */
  46.     KoGuides( KoView *view, KoZoomHandler *zoomHandler );
  47.  
  48.     /**
  49.      * @brief Destructor
  50.      */
  51.     ~KoGuides();
  52.         
  53.     /**
  54.      * @brief Paint the guides
  55.      *
  56.      * @param painter with which the guides are painted
  57.      */
  58.     void paintGuides( QPainter &painter );
  59.  
  60.     typedef int SnapStatus;
  61.     static const SnapStatus SNAP_NONE, SNAP_HORIZ, SNAP_VERT, SNAP_BOTH;
  62.  
  63.     /**
  64.      * @brief Handle mousePressEvent
  65.      *
  66.      * This checks if a mousePressEvent would affect a guide line.
  67.      * If the mouse is pressed over a guide line it gets selected.
  68.      * Guide lines which were select get unselect.
  69.      * If also the Ctrl Key is pressed the selection of the guide 
  70.      * gets toggled.
  71.      * If no guide is under the position all guides get deselected.
  72.      *
  73.      * @param e QMouseEvent
  74.      *
  75.      * @return true if the event was handled
  76.      * @return false otherwise
  77.      *         The event counts a not handled when only guides where
  78.      *         deselected.
  79.      */
  80.     bool mousePressEvent( QMouseEvent *e );
  81.  
  82.     /**
  83.      * @brief Handle mouseMoveEvent
  84.      *
  85.      * If the mouse button is pressed and a guide was selected it moves the 
  86.      * selected guides.
  87.      * If the mouse is moved over a guide line the cursor gets updated.
  88.      *
  89.      * @param e QMouseEvent
  90.      *
  91.      * @return true if the event was handled (guide moved, cursor changed as
  92.      *         guide lies below)    
  93.      * @return false otherwise
  94.      */
  95.     bool mouseMoveEvent( QMouseEvent *e );
  96.     
  97.     /**
  98.      *
  99.      * @param e QMouseEvent
  100.      *
  101.      * @return true if the event was handled
  102.      * @return false otherwise
  103.      */
  104.     bool mouseReleaseEvent( QMouseEvent *e );
  105.     
  106.     /**
  107.      *
  108.      * @param e QKeyEvent
  109.      *
  110.      * @return true if the event was handled
  111.      * @return false otherwise
  112.      */
  113.     bool keyPressEvent( QKeyEvent *e );
  114.  
  115.     /**
  116.      * @brief Set the guide lines.
  117.      *
  118.      * This removes all existing guides and set up new ones at the positions given.
  119.      *
  120.      * @param horizontalPos A list of the position of the horizontal guide lines.
  121.      * @param verticalPos A list of the position of the vertical guide lines.
  122.      */
  123.     void setGuideLines( const QValueList<double> &horizontalPos, const QValueList<double> &verticalPos );
  124.  
  125.     /**
  126.      * @brief Set the positions for snapping of auto guide lines
  127.      *
  128.      * This removes all existing auto guide lines and set up new ones at the positions given.
  129.      *
  130.      * @param horizontalPos A list of the position of the horizontal guide lines.
  131.      * @param verticalPos A list of the position of the vertical guide lines.
  132.      */
  133.     void setAutoGuideLines( const QValueList<double> &horizontalPos, const QValueList<double> &verticalPos );
  134.  
  135.     /**
  136.      * @brief Get the position of the guide lines
  137.      *
  138.      * This filles the passed lists with the positions of the guide lines. 
  139.      * The lists will be emptied before any positions are added.
  140.      *
  141.      * @param horizontalPos A list of the position of the horizontal guide lines.
  142.      * @param verticalPos A list of the position of the vertical guide lines.
  143.      */
  144.     void getGuideLines( QValueList<double> &horizontalPos, QValueList<double> &verticalPos ) const;
  145.  
  146.     /**
  147.      * @brief Snap rect to guidelines
  148.      *
  149.      * This looks for a guide which is in reach for the guide as defined in snap.
  150.      * This method has the abillity to combine more calls. The snapStatus and diff args are both input and
  151.      * output. On first call you should set snapStatus to 0. The return value would then show in which
  152.      * directions it has snapped. If you combine several KoGuides you can let these output arguments
  153.      * be input for the next koGuide. That way you'll always catch the nearest guide.
  154.      *
  155.      * @param rect the rect which should be snapped
  156.      * @param snap the distance within the guide should snap - but always snap if already snapped
  157.      * @param snapStatus if horiz,vert or both directions are snapped (both in and out param). 
  158.      * @param diff distance away from guide. Only valid if status is snapping (both in and out param)
  159.      */
  160.     void snapToGuideLines( KoRect &rect, int snap, SnapStatus &snapStatus, KoPoint &diff );
  161.  
  162.     /**
  163.      * @brief Snap rect to guidelines
  164.      *
  165.      * This looks fo a guide which is in reach for the guide as defined in snap.
  166.      *
  167.      * @param pos the position which should be snapped
  168.      * @param snap the distance wherein the guide should snap - but always snap if already snapped
  169.      * @param snapStatus if horiz,vert or both directions are snapped (both in and out param)
  170.      * @param diff distance away from guide. Only valid if status is snapping (both in and out param)
  171.      */
  172.     void snapToGuideLines( KoPoint &pos, int snap, SnapStatus &snapStatus, KoPoint &diff );
  173.  
  174.     /**
  175.      * @brief repaint guides if any changed snapping status
  176.      *
  177.      * This issues a paint request if any guides have changed snapping status.
  178.      *
  179.      * @param snappedRect the rect after it has been snapped
  180.      */
  181.     void repaintSnapping( const KoRect &snappedRect );
  182.  
  183.     /**
  184.      * @brief repaint guides if any changed snapping status
  185.      *
  186.      * This issues a paint request if any guides have changed snapping status.
  187.      *
  188.      * @param snappedPoint the point after it has been snapped
  189.      */
  190.     void repaintSnapping( const KoPoint &snappedPoint, SnapStatus snapStatus );
  191.  
  192.     /**
  193.      * @brief repaint guides so none is snapped
  194.      *
  195.      * This issues a paint request if any guides have changed snapping status.
  196.      * It also effectively un-snaps all since it doesn't take an argument 
  197.      */
  198.     void repaintAfterSnapping( );
  199.  
  200.     /**
  201.      * @brief Find the closesed disance to the next guide within the given distance
  202.      *
  203.      * @param rect The rect which should be snapped
  204.      * @param diff distance in which too look for the closesed guide. The parameter is updated
  205.      * with the closesed distance to a guide if one is found (both in and out param)
  206.      */
  207.     void diffNextGuide( KoRect &rect, KoPoint &diff );
  208.  
  209. public slots:
  210.     /**
  211.      * @brief Move Guide
  212.      *
  213.      * This slot can be connected to void KoRuler::moveGuide( const QPoint &, bool, int );
  214.      * It will add a new guide when you move from the ruler to the canvas. After that it
  215.      * moves the guide.
  216.      *
  217.      * @param pos The pos of the mouse 
  218.      * @param horizontal true if the guide is horizontal, false if vertical
  219.      * @param rulerWidth The witdth of the ruler as the pos is seen from the ruler widget.
  220.      */
  221.     void moveGuide( const QPoint &pos, bool horizontal, int rulerWidth );
  222.  
  223.     /**
  224.      * @brief Add Guide
  225.      *
  226.      * This slot can be connected to void KoRuler::addGuide( const QPoint &, bool, int );
  227.      * It will finish the inserting of a guide from moveGuide().
  228.      *
  229.      * @param pos The pos of the mouse 
  230.      * @param horizontal true if the guide is horizontal, false if vertical
  231.      * @param rulerWidth The witdth of the ruler as the pos is seen from the ruler widget.
  232.      */
  233.     void addGuide( const QPoint &pos, bool horizontal, int rulerWidth );
  234.  
  235. signals:
  236.     /**
  237.      * @brief Signal that shows that the guide lines are changed
  238.      *
  239.      * This signal is emmited when the guide lines are changed ( moved / deleted )
  240.      *
  241.      * @param view The view in which the guide lines are changed.
  242.      */
  243.     void guideLinesChanged( KoView * view );
  244.  
  245.     /**
  246.      * @brief This signal is emitted when guides start/stop moving.
  247.      *
  248.      * @param state true when starting moving guides, false when stopping.
  249.      */
  250.     void moveGuides( bool state );
  251.  
  252.     /**
  253.      * @brief This signal is emitted when guides start/stop painting.
  254.      *
  255.      * With this signal it is possible to only repaint the guides in the paint 
  256.      * method of the canvas. Just set/unset a flag when this signal is emmited.
  257.      * This signal is emitted before and after a repaint is done.
  258.      *
  259.      * @param state true when starting painting guides, false when stopping.
  260.      */
  261.     void paintGuides( bool state );
  262.  
  263. private slots:
  264.     /**
  265.      * @brief Execute a dialog to set the position of the guide
  266.      */
  267.     void slotChangePosition();
  268.  
  269.     /**
  270.      * @brief remove all selected guides
  271.      */
  272.     void slotRemove();
  273.  
  274. private:
  275.     /// Strukt holding the data of a guide line
  276.     struct KoGuideLine 
  277.     {
  278.         KoGuideLine( Qt::Orientation o, double pos, bool a = false )
  279.         : orientation( o )
  280.         , position( pos )
  281.         , selected( false )
  282.         , snapping( false )
  283.         , automatic( a )
  284.         {}
  285.         Qt::Orientation orientation;
  286.         double position;
  287.         bool selected; // if this guide is selected
  288.         bool snapping; // if this guide is being snapped to
  289.         bool automatic; // if this is a atomatic guide line
  290.     };
  291.  
  292.     /**
  293.      * @brief Paint the canvas
  294.      */ 
  295.     void paint();
  296.  
  297.     /**
  298.      * @brief Add a guide line with the orientation o at the position pos
  299.      *
  300.      * @param pos where to insert the guide
  301.      * @param o orientation of the guide line
  302.      */
  303.     void add( Qt::Orientation o, QPoint &pos );
  304.     
  305.     /**
  306.      * @brief Select a guide
  307.      *
  308.      * @param gd guide to select
  309.      */
  310.     void select( KoGuideLine *guideLine );
  311.  
  312.     /**
  313.      * @brief Unselect a guide
  314.      *
  315.      * @param gd guide to unselect
  316.      */
  317.     void unselect( KoGuideLine *guideLine );
  318.         
  319.     /**
  320.      * @brief Unselect all selected KoGuideLineData
  321.      *
  322.      * @return true, when selection was changed
  323.      * @return false otherwise
  324.      */
  325.     bool unselectAll();
  326.  
  327.     /**
  328.      * @brief remove all selected guides
  329.      */
  330.     void removeSelected();
  331.  
  332.     /**
  333.      * @brief Check if at least one guide is selected
  334.      *
  335.      * @return true if at least on guide is seleted
  336.      * @return false otherwise
  337.      */
  338.     bool hasSelected();
  339.  
  340.     /**
  341.      * @brief Find a guide
  342.      *
  343.      * This function looks for a guide at x or y pos. The position can differ by
  344.      * diff.
  345.      *
  346.      * @param x x position to look for a guide
  347.      * @param y y position to look for a guide
  348.      * @param diff how far next to a guide sould it also be found
  349.      *
  350.      * @return the fould guide
  351.      * @return 0 if none is found
  352.      */
  353.     KoGuideLine * find( KoPoint &p, double diff );
  354.  
  355.     /**
  356.      * @brief Move selected guides.
  357.      *
  358.      * This moves all selected guides around. If more than one guide is selected it makes
  359.      * sure the guides are not moved of the canvas.
  360.      *
  361.      * @param pos position of the mouse
  362.      */
  363.     void moveSelectedBy( QPoint &p );
  364.  
  365.     /**
  366.      * @brief Map pos from screen
  367.      * 
  368.      * @param pos on screen
  369.      *
  370.      * @return pos in document
  371.      */
  372.     KoPoint mapFromScreen( const QPoint & pos );
  373.  
  374.     /**
  375.      * @brief Map pos to screen
  376.      * 
  377.      * @param pos in document
  378.      *
  379.      * @return pos on screen
  380.      */
  381.     QPoint mapToScreen( const KoPoint & pos );
  382.  
  383.     /**
  384.      * @brief Check if the both values are nearly the same.
  385.      *
  386.      * @param a first value
  387.      * @param a second value
  388.      *
  389.      * @return true if they are the same
  390.      * @return false otherwise
  391.      */ 
  392.     bool virtuallyEqual( double a, double b ) { return QABS( a - b ) < 1E-4; }
  393.  
  394.     /// view
  395.     KoView * m_view;
  396.     /// zoom handler of the view
  397.     KoZoomHandler * m_zoomHandler;
  398.  
  399.     enum GuideLineType
  400.     {
  401.         GL,
  402.         GL_SELECTED, 
  403.         GL_AUTOMATIC,
  404.         GL_END
  405.     };
  406.  
  407.     /// array of list of the different guide line types
  408.     QValueList<KoGuideLine *> m_guideLines[GL_END];
  409.     
  410.     /// used to save the last mouse position
  411.     QPoint m_lastPoint;
  412.     /// true if a guide is selected at the moment
  413.     bool m_mouseSelected;
  414.     /// true if a guide is inserted at the moment
  415.     bool m_insertGuide;
  416.     /// popup menu
  417.     class Popup;
  418.     Popup * m_popup;
  419. };
  420.  
  421. #endif /* KOGUIDES_H */
  422.